package st.business.tepth.business.user.busnumber.workholder.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;

import java.util.ArrayList;
import java.util.List;

import st.business.tepth.R;
import st.business.tepth.business.bean.PartBean;
import st.business.tepth.business.bean.PersonBean;
import st.business.tepth.business.bean.ProgramBean;
import st.business.tepth.config.ItemFields;
import st.business.tepth.manager.UserManager;
import st.shadow.library.ui.recycler.MultipleItemEntity;
import st.shadow.library.ui.recycler.MultipleViewHolder;

/**
 * @author zhangyu
 * @date 2019/2/15
 */
public class TroubleProgramAdapter extends BaseMultiItemQuickAdapter<MultipleItemEntity,
        MultipleViewHolder> {
    //关联过来的方案未提交
    public static final int TYPE_RELEVANCE_UN_CONFIRM = 100;
    //系统推荐未确认
    public static final int TYPE_SYSTEM_RECOMMEND_UN_CONFIRM = 101;
    //手动添加的方案已确认
    public static final int TYPE_MANUAL_CONFIRM = 102;
    //手动添加的方案未确认
    public static final int TYPE_MANUAL_UN_CONFIRM = 105;
    //整个状态已完成就只能查看了
    public static final int TYPE_CAN_NOT_EDIT = 106;

    private Drawable mDrawableRecommend;
    private Drawable mDrawableRelevance;
    private onItemOperatorListener mDeleteListener;
    private onItemOperatorListener mAddWorkerListener;
    private onItemOperatorListener mCancelWorkerListener;

    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     *
     * @param data A new list is created out of this one to avoid mutable list
     */
    public TroubleProgramAdapter(Context context, List<MultipleItemEntity> data,
                                 onItemOperatorListener deleteListener,
                                 onItemOperatorListener addWorkerListener,
                                 onItemOperatorListener cancelWorkerListener) {
        super(data);
        addItemType(TYPE_RELEVANCE_UN_CONFIRM, R.layout.item_work_program_relevance_un_confirm);
        addItemType(TYPE_SYSTEM_RECOMMEND_UN_CONFIRM, R.layout.item_work_program_my_edit);
        addItemType(TYPE_MANUAL_CONFIRM, R.layout.item_work_program_done_my);
        addItemType(TYPE_MANUAL_UN_CONFIRM, R.layout.item_work_program_my_edit);
        addItemType(TYPE_CAN_NOT_EDIT, R.layout.item_work_program_can_not_edit);
        mDrawableRecommend = context.getResources().getDrawable(R.drawable.ic_recommend);
        mDrawableRecommend.setBounds(0, 0,
                mDrawableRecommend.getMinimumWidth(), mDrawableRecommend.getMinimumHeight());
        mDrawableRelevance = context.getResources().getDrawable(R.drawable.ic_relevance);
        mDrawableRelevance.setBounds(0, 0,
                mDrawableRelevance.getMinimumWidth(), mDrawableRelevance.getMinimumHeight());
        mDeleteListener = deleteListener;
        mAddWorkerListener = addWorkerListener;
        mCancelWorkerListener = cancelWorkerListener;
    }

    @Override
    protected void convert(final MultipleViewHolder helper, final MultipleItemEntity item) {
        final ProgramBean programBean = item.getField(ItemFields.ITEM_OBJ);
        helper.setText(R.id.tv_trouble, programBean.getDiagnostic_name());
        helper.setText(R.id.tv_place, programBean.getPositionName());
        List<PersonBean> personBeans = programBean.getPersonList();
        if (personBeans == null) {
            personBeans = new ArrayList<>();
            personBeans.add(new PersonBean(UserManager.getInstance().getCurrentUserName(),
                    UserManager.getInstance().getCurrentUserCode()));
            programBean.setPersonList(personBeans);
        }
        helper.setText(R.id.tv_work_person, convertPersonList(programBean.getMainPerson(),
                personBeans));
        helper.setText(R.id.tv_action, programBean.getAction_name());
        PartBean part = programBean.getPart();
        helper.setText(R.id.tv_obj, part == null ? "无" : part.getMaterial_name());
        helper.setText(R.id.tv_part_code, part == null ? "无" : part.getMaterial_no());
        helper.setText(R.id.tv_count, part == null ? "1" : String.valueOf(part.getVal()));
        helper.setText(R.id.tv_unit, part == null ? "无" : part.getUnit());

        PartBean material = programBean.getMaterial();
        helper.setText(R.id.tv_materials, material == null ?
                "无" : material.getMaterial_name());

        helper.setText(R.id.tv_materials_code, material == null ?
                "无" : material.getMaterial_no());
        helper.setText(R.id.tv_materials_count, material == null ? "0" :
                (material.getVal() + (material.getUnit() == null ? "" :
                        material.getUnit())));
        helper.setText(R.id.tv_goods, getPickStatus(programBean.getAction_name(), programBean.getPicking_status()));
        TextView tvTrouble = helper.getView(R.id.tv_trouble);
        tvTrouble.setCompoundDrawables(null, null, null, null);
        switch (item.getItemType()) {
            case TYPE_RELEVANCE_UN_CONFIRM:
                tvTrouble.setCompoundDrawables(mDrawableRelevance,
                        null, null, null);
                helper.addOnClickListener(R.id.tv_delete);
                helper.setText(R.id.tv_confirm, "未确认");
                helper.setText(R.id.tv_goods, "未审批");
                helper.setText(R.id.tv_delete, "删除");
                break;
            case TYPE_SYSTEM_RECOMMEND_UN_CONFIRM:
                tvTrouble.setCompoundDrawables(mDrawableRecommend,
                        null, null, null);
            case TYPE_MANUAL_UN_CONFIRM:
                helper.addOnClickListener(R.id.tv_action);
                helper.addOnClickListener(R.id.tv_obj);
                helper.addOnClickListener(R.id.tv_part_code);
                helper.addOnClickListener(R.id.tv_count);
                helper.addOnClickListener(R.id.tv_materials);
                helper.addOnClickListener(R.id.tv_materials_code);
                helper.addOnClickListener(R.id.tv_materials_count);
                helper.addOnClickListener(R.id.tv_place);
                helper.addOnClickListener(R.id.tv_delete);
                helper.setText(R.id.tv_confirm, "未确认");
                helper.setText(R.id.tv_goods, "未审批");
                helper.setText(R.id.tv_delete, "删除");
                break;
            case TYPE_MANUAL_CONFIRM:
                final TextView tvOperation = helper.getView(R.id.tv_operation);
                if (programBean.getRelation_state() == 1) {
                    //关联的方案确认后只能删除
                    tvTrouble.setCompoundDrawables(mDrawableRelevance,
                            null, null, null);
                    tvOperation.setText("删除");
                    tvOperation.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (mDeleteListener != null) {
                                mDeleteListener.onOperator(programBean, "删除",
                                        helper.getAdapterPosition());
                            }
                        }
                    });
                    return;
                } else {
                    tvTrouble.setCompoundDrawables(null,
                            null, null, null);
                }
                helper.setText(R.id.tv_confirm, "已确认");

                PersonBean mainPerson = programBean.getMainPerson();
                if (mainPerson != null) {
                    if (UserManager.getInstance()
                            .getCurrentUserCode().equals(mainPerson.getJob_no())) {
                        //是主作业人
                        final String status = getOperation(programBean.getPicking_status());
                        tvOperation.setText(status);
                        tvOperation.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                int pickStatus = programBean.getPicking_status();
                                if (pickStatus != 1 && pickStatus != 2
                                        && pickStatus != 4 && pickStatus <= 9) {
                                    if (mDeleteListener != null) {
                                        mDeleteListener.onOperator(programBean, status,
                                                helper.getAdapterPosition());
                                    }
                                }
                            }
                        });
                    } else {
                        //非主作业人
                        if (hasMe(personBeans)) {
                            tvOperation.setText("-取消我");
                            tvOperation.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    if (mCancelWorkerListener != null) {
                                        mCancelWorkerListener.onOperator(programBean, null,
                                                helper.getAdapterPosition());
                                    }
                                }
                            });
                        } else {
                            tvOperation.setText("+添加我");
                            tvOperation.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    if (mAddWorkerListener != null) {
                                        mAddWorkerListener.onOperator(programBean, null,
                                                helper.getAdapterPosition());
                                    }
                                }
                            });
                        }
                    }
                }
                break;
            case TYPE_CAN_NOT_EDIT:
                final TextView tvOperationDelete = helper.getView(R.id.tv_operation);
                if (programBean.getRelation_state() == 1) {
                    //关联的方案确认后只能删除
                    tvTrouble.setCompoundDrawables(mDrawableRelevance,
                            null, null, null);
                    tvOperationDelete.setText("删除");
                }
                break;

            default:
                break;
        }
    }

    private boolean hasMe(List<PersonBean> personBeans) {
        String myCode = UserManager.getInstance().getCurrentUserCode();
        for (PersonBean personBean : personBeans) {
            if (myCode.equals(personBean.getJob_no())) {
                return true;
            }
        }
        return false;
    }

    private String convertPersonList(PersonBean mainPerson, List<PersonBean> list) {
        StringBuilder str = new StringBuilder();
        if (mainPerson != null) {
            str.append(mainPerson.getUser_name()).append(",");
        }
        if (list != null) {
            for (PersonBean personBean : list) {
                str.append(personBean.getUser_name()).append(",");
            }
        }
        if (str.length() > 0) {
            str = new StringBuilder(str.substring(0, str.length() - 1));
        } else {
            str = new StringBuilder("无");
        }
        return str.toString();
    }

    public boolean hasNoPart() {
        List<MultipleItemEntity> all = getData();
        for (MultipleItemEntity entity : all) {
            ProgramBean diagnoseBean = entity.getField(ItemFields.ITEM_OBJ);
            if (diagnoseBean.getPart() == null) {
                return true;
            }
        }
        return false;
    }

    public String getPickStatus(String action, int status) {

        String str = "";
        switch (status) {
            case 0:
                str = "待申请";
                break;
            case 1:
                str = "待审批";
                break;
            case 2:
                str = "待领料";
                break;
            case 3:
                str = "已领料";
                break;
            case 4:
                str = "退料中";
                break;
            case 5:
                str = "退料失败";
                break;
            case 6:
                str = "已退料";
                break;
            case 7:
                str = "已取消";
                break;
            case 8:
                str = "被驳回";
                break;
            case 9:
                str = "已过期";
                break;
            case 10:
                str = "待料待审批";
                break;
            case 11:
                str = "待料中";
                break;
            case 12:
                str = "待料被驳回";
                break;
            case 13:
                str = "审批中";
                break;
            default:
                str = "--";
                break;
        }
        switch (action) {
            case "拆检":
            case "拆装":
            case "打胶":
            case "调校":
            case "调整":
            case "焊接":
            case "检查":
            case "紧固":
            case "喷漆":
            case "清洁":
            case "清理":
            case "润滑":
                str = "--";
                break;
        }
        return str;
    }

    public String getOperation(int status) {
        String str = "";
        switch (status) {
            case 1:
            case 2:
            case 4:
            case 10:
            case 11:
            case 12:
            case 13:
                str = "无";
                break;
            case 3:
            case 5:
                str = "退料";
                break;
            case 0:
            case 6:
            case 7:
            case 8:
            case 9:
                str = "删除";
                break;
        }
        return str;
    }

    @Override
    protected MultipleViewHolder createBaseViewHolder(View view) {
        return MultipleViewHolder.create(view);
    }

    public interface onItemOperatorListener {
        void onOperator(ProgramBean program, String status, int position);
    }

}
